package com.atguigu.blog.service.impl;

import com.atguigu.blog.mapper.CategoryMapper;

import com.atguigu.blog.pojo.Category;
import com.atguigu.blog.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CategoryServiceImpl
 * @Description TODO
 * @Author 孤妤
 * @Date 2023/11/24 16:56
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    @Transactional(readOnly = true)
    public List<Category> showAll() {
        return categoryMapper.selectAll();
    }

    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> showAllPage(Integer page, Integer limit, Category category) {
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<Category> categoryList = categoryMapper.selectAllPage(category);
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        map.put("list",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    @Override
    @Transactional(readOnly = true)
    public Category showAllByCid(Integer cid) {
        return categoryMapper.selectAllByCid(cid);
    }

    @Override
    public Integer add(Category category) {
        return categoryMapper.insert(category);
    }

    @Override
    public Integer change(Category category) {
        return categoryMapper.update(category);
    }

    @Override
    public Integer remove(Integer cid) {
        return categoryMapper.delete(cid);
    }
}